package com.example.demo;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.annotation.FieldFill;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.InjectionConfig;
import com.baomidou.mybatisplus.generator.config.*;
import com.baomidou.mybatisplus.generator.config.po.TableFill;
import com.baomidou.mybatisplus.generator.config.po.TableInfo;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;
import com.baomidou.mybatisplus.generator.engine.FreemarkerTemplateEngine;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import java.util.*;

@SpringBootApplication
public class DemoApplication {
    public static int scanner() {
        Scanner scanner = new Scanner(System.in);
        StringBuilder h = new StringBuilder();
        h.append("!!代码生成，输入0表示使用Velocity引擎！！");
        h.append("\n对照表");
        h.append("\n0= Velocity 引擎");
        h.append("\n1 = Freemarker 引擎");
        h.append("\n请输入");
        System.out.println(h.toString());
        int slt = 0;
        //现在有输入数据
        if(scanner.hasNext()){
            String ipt = scanner.next();
            if("1".equals(ipt)){
                slt=1;
            }
        }
        return slt;
    }
    public static void main(String[] args) {
        int result =scanner();
        //自定义需要填充的字段
        List<TableFill> tableFillList = new ArrayList<TableFill>();
        tableFillList.add(new TableFill("create_user", FieldFill.INSERT));
        tableFillList.add(new TableFill("update_user",FieldFill.UPDATE));
        tableFillList.add(new TableFill("create_time",FieldFill.INSERT));
        tableFillList.add(new TableFill("update_time",FieldFill.UPDATE));

        //代码生成器
        AutoGenerator mpg = new AutoGenerator().setGlobalConfig(
                new GlobalConfig()
                        .setOutputDir("D:\\") //输出目录
                        .setFileOverride(true) //是否覆盖文件
                        .setActiveRecord(true) //开启activeRecord模式
                        .setEnableCache(false) //xml 二级缓存
                        .setBaseResultMap(true) //xml resultMap
                        .setBaseColumnList(true) //xml columList
                        .setMapperName("%sMapper")
                        .setXmlName("%sMapper")
                        .setServiceName("%sService")
                        .setServiceImplName("%sServiceImpl")
                        .setControllerName("%sController")

        ).setDataSource(
                new DataSourceConfig()
                        .setDriverName("com.mysql.cj.jdbc.Driver")
                        .setUsername("root")
                        .setPassword("123456")

                        .setUrl("jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false&serverTimezone=UTC")
        ).setPackageInfo(
                new PackageConfig()
                        .setModuleName("config")
                        .setParent("com.example.demo.sys")
                        .setController("controller")
        ).setCfg(
                new InjectionConfig() {
                    @Override
                    public void initMap() {
                        Map<String,Object> map = new HashMap<>();
                        map.put("abc",this.getConfig().getGlobalConfig().getAuthor()+"-mp");
                        this.setMap(map);

                    }
                }.setFileOutConfigList(Collections.<FileOutConfig>singletonList(new FileOutConfig(
                        "/templates/mapper.xml" + ((1 == result) ? ".ftl" : ".vm")) {
                    @Override
                    public String outputFile(TableInfo tableInfo) {
                        return "D:/mapping/" + tableInfo.getEntityName() + "Mapper.xml";

                    }
                }))


        ).setTemplate(
                new TemplateConfig().setXml(null)
        );
        StrategyConfig strategyConfig= new StrategyConfig();
        strategyConfig
                .setEntityLombokModel(true)
                .setTablePrefix("sys_")
                .setNaming(NamingStrategy.underline_to_camel)
                .setInclude("sys_config")
                .setSuperEntityClass("com.common.platform.sys.base.pojo.BaseEntity")
                .setSuperEntityColumns("create_time","update_time","create_user","update_user")
                .setSuperControllerClass("com.common.platform.sys.base.controller.BaseController");
        mpg.setStrategy(strategyConfig);
        //执行生成
        if(1 == result){
            mpg.setTemplateEngine(new FreemarkerTemplateEngine());
        }
        mpg.execute();

        System.out.println(mpg.getCfg().getMap().get("abc"));

    }

}


